/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * ThreeScaleInTransformer
 *
 * @since 2021-08-09
 */
public class ThreeScaleInTransformer implements BannerViewPager.PageTransformer {
    private static final float NUM_F_0_3 = 0.3f;
    private static final float NUM_F_0_8 = 0.8f;
    private static final float NUM_F_0_95 = 0.95f;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float scale = NUM_F_0_8;
        float scaleX = NUM_F_0_95;
        switch (pageType) {
            case PREVIOUS:
                if (offsetPixels <= 0) {
                    scale = NUM_F_0_8 + offset * NUM_F_0_3;
                    scaleX = scaleX + offset * NUM_F_0_3;
                    if (scale > 1) {
                        scale = 1;
                    }

                    if (1 < scaleX) {
                        scaleX = 1;
                    }
                }
                break;
            case CURRENT:
                scale = 1 - offset * NUM_F_0_3;
                scaleX = 1 - offset * NUM_F_0_3;
                if (NUM_F_0_8 > scale) {
                    scale = NUM_F_0_8;
                }
                if (NUM_F_0_95 > scaleX) {
                    scaleX = NUM_F_0_95;
                }
                break;
            case NEXT:
                if (offsetPixels >= 0) {
                    scale = NUM_F_0_8 + offset * NUM_F_0_3;
                    scaleX = scaleX + offset * NUM_F_0_3;
                    if (scale > 1) {
                        scale = 1;
                    }
                    if (1 < scaleX) {
                        scaleX = 1;
                    }
                }
                break;
            default:
                break;
        }
        page.setScale(scaleX, scale);
    }
}
